<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Home\Controller;
use Think\Controller;
class CenterController extends HomeController {
      public function center(){
        $map = array('id'=>session("userid"));
        $map2 = array('uid'=>session("userid"));
        $order=M('order');
        $orderlist=$order->where($map2)->order('createdate desc')->select();
        $buyer=M("buyers")->where($map)->find();
        $count=count($orderlist);
        $this->assign('userlogo',session('userlogo'));
        $this->assign('count',$count);
        $this->assign('buyer',$buyer);
        $mapht['id']=session('USER')['id'];
        $resht=M('buyers')->where($mapht)->find();
        $contractlevel=$resht['contract_level'];
        $this->assign('contractlevel',$contractlevel);
        $this->display();
    }
    
    public function history(){
        $map = array('uid'=>session("userid"));
        $order=M('order');
        $orderlist=$order->where($map)->order('createdate desc')->select();  
        $goods=M('order_goods')->join('RIGHT JOIN gr_order on gr_order_goods.orderid=gr_order.orderid')->where($map)->select();
        $this->assign('goodslist',$goods);
        $this->assign('historyList',$orderlist);         
        $this->display();
    }   
    
    public function cancelOrder($id){
            $userid=session("userid");
            $orderid=$id;
            if(isset($orderid)){
                $map['orderid'] = $orderid;
                $goodsTable=D('product');
                $res=D('order_goods')->where($map)->select();
                foreach ($res as $value){
                    $data2['id']=$value['pid'];
                    $goodsTable->where($data2)->setInc('inventory',$value['cartnum']);    
                }
                $map['status'] = 1000;
                $orderRes=M('order')->where($map)->find();
                if($orderRes["useamount"]>0){
                    //撤销订单返还余额 start
//                    $detail="用户撤销订单".$orderid;
//                    $act="incre";
//                    R("Activity/amountFlow",array($orderRes["useamount"],$detail,$act));
                    $amountData['id']=$userid;
                    D("buyers")->where(array('id'=>$userid))->setInc('amount',$orderRes["useamount"]);
                    //end
                }
                $data['status']= -1;
                $result= M('order')->where($map)->save($data);
                $limitTable = M("product_limit");
                $limitTable->where(array('orderid'=>$orderid))->delete();
                if($result){
                    $this->redirect('Center/history');
                }else{
                    $this->redirect('Center/history');
                }            
            }                
    }

    public function applyconfirm(){
        if(IS_POST){
            $data['uid']=session('USER')['id'];
            $data['createAt']=NOW_TIME;
            $map['uid']=session('USER')['id'];
            $res=M('contractapply')->where($map)->find();
            if($res){
                $this->display('applyfalse');
            }else{
                $model = M('contractapply');
                $id = $model->add($data);
                if (!$id) {
                    $this->display('applydatafalse');
                } else {
                    $this->display('applysuccess');
                }
            }
        }else{
            $this->display();
        }
    }

}
